<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "pre_admin_article".
 *
 * @property integer $article_id
 * @property integer $aid
 * @property string $article_label
 * @property string $article_text
 * @property string $post_time
 * @property string $update_time
 * @property integer $status
 * @property string $article_title
 */
class PreAdminArticle extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'pre_admin_article';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['aid', 'article_text', 'post_time', 'article_title'], 'required'],
            [['aid', 'status'], 'integer'],
            [['article_text'], 'string'],
            [['post_time', 'update_time'], 'safe'],
            [['article_label', 'article_title'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'article_id' => '文章ID',
            'aid' => '管理员ID',
            'article_label' => '文章标签',
            'article_text' => '内容',
            'post_time' => '发布时间 ',
            'update_time' => '更新时间',
            'status' => '状态',
            'article_title' => '标题',
        ];
    }
	
	public function getPages($query,$curPage=1,$pageSize=10,$search=null){
		if($search)
			$query=$query->andFilerWhere($search);
		$data['count']=$query->count();
		if(!$data['count']){
			return ['count'=>0,'curPage'=>$curPage,'pageSize'=>$pageSize,'start'=>0,
			'end'=>0,'data'=>[],
			];
		}
		$curPage =(ceil($data['count']/$pageSize)<$curPage)
		?ceil($data['count']/$pageSize):$curPage;
		
		$data['curPage']=$curPage;
		$data['pageSize']=$pageSize;
	    $data['start']=($curPage-1)*$pageSize+1;
		$data['end']=(ceil($data['count']/$pageSize)==$curPage)
		   ?$data['count']:($curPage-1)*$pageSize+$pageSize;
		$data['data']=$query
		              ->offset(($curPage-1)*$pageSize)
					  ->limit($pageSize)
					  ->asArray()
					  ->all();
					  
		return $data;
		
	}
}
